/**
	Copyright (c) 2010 ATTi 
	All rights reserved.
	Author: Michael Shu
	Date: 1/26/2010
*/


trigger EventWithOpportunity on Event (before delete, before insert, before update) {
	String LOCAL_ADVERTISER_OPPORTUNITY = 'Local Advertiser';
	String LOCAL_ADVERTISER_CLOSED_OPPORTUNITY = 'Local Advertiser Closed';
	String APPOINTMENT  = 'Appointment';
    String PRESENTATION = 'Presentation';
    Integer i = 0;
	
	ID localAdvertiserEventRecordTypeID = RecordTypeHelper.GetRecordTypeID('Event', 'Local Advertiser Event');
	
	Map<ID, Integer> Appointment_OppIds = new Map<ID, Integer>();
	Map<ID, Integer> Presentation_OppIds = new Map<ID, Integer>();
	Map<ID, DateTime> EventDateTime_OppIds = new Map<ID, DateTime>();

	List<Opportunity> opps = new List<Opportunity>();
	Set<ID> oppIds = new Set<ID>();
		
	//update appointments and presentations in event
	if(trigger.isInsert || trigger.isUpdate){
		for(Event e : trigger.new){
			if(e.RecordTypeId == localAdvertiserEventRecordTypeID){
				e.Appointments__c = 0;
				e.Presentations__c = 0;
				if(e.Type == APPOINTMENT){
					e.Appointments__c = 1;
				}else if(e.Type == PRESENTATION){
					e.Presentations__c = 1;
				}
			}
		}
	}
	
	//get Latest Event Datetime
	if(trigger.isInsert || trigger.isUpdate){
		for(Event e : trigger.new){
			if(e.RecordTypeId == localAdvertiserEventRecordTypeID && e.WhatId != null){
				if(trigger.isInsert || trigger.isUpdate && e.StartDateTime > trigger.oldMap.get(e.Id).StartDateTime){
					if(EventDateTime_OppIds.containsKey(e.WhatId)){
						if(e.StartDateTime > EventDateTime_OppIds.get(e.WhatId) ){
							EventDateTime_OppIds.put(e.WhatId, e.StartDateTime);			
						}
					}else{
						EventDateTime_OppIds.put(e.WhatId, e.StartDateTime);					}
					oppIds.add(e.WhatId);
				}
			}
		}
	}
	
		if(Trigger.isDelete){
			System.debug('Is Delete');
			for(Event e : Trigger.old){
				if(e.RecordTypeId == localAdvertiserEventRecordTypeID && e.WhatId != null){
					if(e.Type == APPOINTMENT ){
						if(Appointment_OppIds.containsKey(e.WhatId)){
							i = Appointment_OppIds.get(e.WhatId);
							Appointment_OppIds.put(e.WhatId, i--);
						}else{
							Appointment_OppIds.put(e.WhatId, -1);
						}
					}else if(e.Type == PRESENTATION){ 
						if(Presentation_OppIds.containsKey(e.WhatId)){
							i = Presentation_OppIds.get(e.WhatId);
							Presentation_OppIds.put(e.WhatId, i--);
						}else{
							Presentation_OppIds.put(e.WhatId, -1);
						}
					}
					oppIds.add(e.WhatId);
				}
			}
		}else if(Trigger.isInsert){
			System.debug('Is Insert');
			for(Event e : Trigger.new){
				if(e.RecordTypeId == localAdvertiserEventRecordTypeID && e.WhatId != null){
					if(e.Type == APPOINTMENT){
						if(Appointment_OppIds.containsKey(e.WhatId)){
							i = Appointment_OppIds.get(e.WhatId);
							Appointment_OppIds.put(e.WhatId, i++);
						}else{
							Appointment_OppIds.put(e.WhatId, 1);
						}
					}else if(e.Type == PRESENTATION){
						if(Presentation_OppIds.containsKey(e.WhatId)){
							i = Presentation_OppIds.get(e.WhatId);
							Presentation_OppIds.put(e.WhatId, i++);
						}else{
							Presentation_OppIds.put(e.WhatId, 1);
						}
					}
					oppIds.add(e.WhatId);			
				}
			}
		}else if(Trigger.isUpdate){
			System.debug('Is Update');
			for(Event e : Trigger.new){
				ID oldWhatId = Trigger.oldMap.get(e.Id).WhatId;
				String oldType = Trigger.oldMap.get(e.Id).Type;
				if(e.RecordTypeId == localAdvertiserEventRecordTypeID){
					if(oldWhatId != e.WhatId || oldType != e.Type){
						if(e.Type == APPOINTMENT){
							if(Appointment_OppIds.containsKey(e.WhatId)){
								i = Appointment_OppIds.get(e.WhatId);
								Appointment_OppIds.put(e.WhatId, i++);
							}else{
								Appointment_OppIds.put(e.WhatId, 1);
							}
						}else if(e.Type == PRESENTATION){
							if(Presentation_OppIds.containsKey(e.WhatId)){
								i = Presentation_OppIds.get(e.WhatId);
								Presentation_OppIds.put(e.WhatId, i++);
							}else{
								Presentation_OppIds.put(e.WhatId, 1);
							}
						}
						if(oldType == APPOINTMENT ){
							if(Appointment_OppIds.containsKey(oldWhatId)){
								i = Appointment_OppIds.get(oldWhatId);
								Appointment_OppIds.put(oldWhatId, i--);
							}else{
								Appointment_OppIds.put(oldWhatId, -1);
							}
						}else if(oldType == PRESENTATION){ 
							if(Presentation_OppIds.containsKey(oldWhatId)){
								i = Presentation_OppIds.get(oldWhatId);
								Presentation_OppIds.put(oldWhatId, i--);
							}else{
								Presentation_OppIds.put(oldWhatId, -1);
							}
						}
						oppIds.add(oldWhatId);
						oppIds.add(e.WhatId);						
					}
				}
			}		
		}
		
		if(!oppIds.isEmpty() ){
			System.debug('Opportunity ID list is not Blank');
			List<Opportunity> temp_opps = [select Id, Num_of_Appointments__c, Num_of_Presentations__c,HasEventScheduled__c,
			                                       Latest_Event_Start_Date__c, Latest_Owner_Change_Date__c  
			                               from Opportunity where Id IN : oppIds 
			                                 and (RecordType.Name =: LOCAL_ADVERTISER_OPPORTUNITY 
			                                   OR RecordType.Name =: LOCAL_ADVERTISER_CLOSED_OPPORTUNITY)];
			for(Opportunity o : temp_opps){
				if(Appointment_OppIds.containsKey(o.Id)){
					if(o.Num_of_Appointments__c == null){
						o.Num_of_Appointments__c = Appointment_OppIds.get(o.Id);
					}else{
					   o.Num_of_Appointments__c += Appointment_OppIds.get(o.Id);
					}
					if(o.Num_of_Appointments__c < 0){
						o.Num_of_Appointments__c = 0;
					}
				}
				if(Presentation_OppIds.containsKey(o.Id)){
					if(o.Num_of_Presentations__c == null){
						o.Num_of_Presentations__c = Presentation_OppIds.get(o.Id);
					}else{
						o.Num_of_Presentations__c += Presentation_OppIds.get(o.Id);
					}
					if(o.Num_of_Presentations__c < 0){
						o.Num_of_Presentations__c = 0;
					}
				}				
				if(EventDateTime_OppIds.containsKey(o.Id)){
					if(o.Latest_Owner_Change_Date__c != null && EventDateTime_OppIds.get(o.Id) >  o.Latest_Owner_Change_Date__c.addDays(30)
					                                         && EventDateTime_OppIds.get(o.Id) <= o.Latest_Owner_Change_Date__c.addDays(60)){
						if(o.Latest_Event_Start_Date__c == null){
							o.Latest_Event_Start_Date__c = EventDateTime_OppIds.get(o.Id);
						}else if(EventDateTime_OppIds.get(o.Id) > o.Latest_Event_Start_Date__c){
							o.Latest_Event_Start_Date__c = EventDateTime_OppIds.get(o.Id);
						}
						o.HasEventScheduled__c = true;
					}
				}
				
				if(Appointment_OppIds.containsKey(o.Id) 
				|| Presentation_OppIds.containsKey(o.Id)
				|| EventDateTime_OppIds.containsKey(o.Id)){
				   	opps.add(o);
				 }
			}
		}

		/*----Test area-----*/
		for(Id a : Appointment_OppIds.keySet()){
			System.debug('Appointment_OppIds : ' + a);
		}
		for(Id b : Presentation_OppIds.keySet()){
			System.debug('Presentation_OppIds : ' + b);
		}
		for(Id c : EventDateTime_OppIds.keySet()){
			System.debug('EventDateTime_OppIds : ' + c);
		}
		for(Opportunity d : opps){
			System.debug('opps Id : ' + d.Id);
		}
		
		/*-----------------*/
		update opps;	
}